#!/usr/bin/env python
# coding: utf-8

# In[1]:


"""搜索代码"""

import os

# 找到指定目录下的代码文件（如.py）
def openfile(path):
    List = []
    for file in os.listdir(path):
        if os.path.splitext(file)[1] == '.py':
            List.append(path +'/'+file)
            # 将python代码文件放入列表
            print('加入',file)
    return List

# 分别筛选代码、空行、注释的行数
def rows(file):
    with open(file,encoding='utf8') as f:
        l= f.readline()    
        r = len(l)   # 统计代码行数
        v =0
        s =0
        # 初始化空行、注释的行数
        
        for line in l:
            if line =='\n': # 判断空行数
                s +=1
            if line[0]=="#":# 判断注释数
                v+=1
                
        # 将统计数值放入字典中
        dic ={
            'r':r,
            'v':v,
            's':s
        }
    return date

# 统计各项行数值
def Open(path):
    List = openfile(path)

    m = 0
    n = 0
    k = 0

    for file in List:
        dic = rows(file)
        print("计算中",file)
        r = dic.get( 'r')
        v = dic.get('v')
        s = dic.get('s')
        m += r
        n += s
        k += v
    print('代码行数：{}\n注释行数：{}\n空行行数：{}'.format(m, n, k))

# 指定目录并进行代码统计
path = 'C:\\Users'
Open(path=path)


# In[ ]:




